package com.ojoin.ojoin.eb_spider.unZip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZipDecompressingUtils {

    private static Logger log = LoggerFactory
            .getLogger(ZipDecompressingUtils.class);

    public static void unZip(String zip, String filePath, Charset charset) {
        File file = new File(zip);
        unZip(file, filePath,charset);
    }

    public static void unZip(String zip, String filePath) {
        unZip(zip,filePath,StandardCharsets.UTF_8);
    }

    public static void unZip(File file, String filePath){
        unZip(file,filePath,StandardCharsets.UTF_8);
    }

    public static void unZip(File file, String filePath,Charset charset) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (int i = 0; i < files.length; i++) {
                    unZip(files[i], filePath,charset);
                }
            }
        } else {
            if (file.getName().endsWith("zip")) {
                unZipFile(file, filePath,charset);
            }
        }
    }

    public static void unZipFile(File file, String filePath){
        unZipFile(file,filePath,StandardCharsets.UTF_8);
    }

    public static void unZipFile(File file, String filePath,Charset charset) {
        long startTime = System.currentTimeMillis();
        try {
            // "C:\\Users\\HAN\\Desktop\\stock\\SpectreCompressed.zip"
            // String Parent="C:\\Users\\HAN\\Desktop"; //输出路径（文件夹目录）

            ZipInputStream Zin = new ZipInputStream(new FileInputStream(file),charset);//输入源zip路径
            BufferedInputStream Bin = new BufferedInputStream(Zin);
            File Fout = null;
            ZipEntry entry;
            try {
                while ((entry = Zin.getNextEntry()) != null && !entry.isDirectory()) {
                    Fout = new File(filePath, entry.getName());
                    if (!Fout.exists()) {
                        (new File(Fout.getParent())).mkdirs();
                    }
                    FileOutputStream out = new FileOutputStream(Fout);
                    BufferedOutputStream Bout = new BufferedOutputStream(out);
                    int b;
                    while ((b = Bin.read()) != -1) {
                        Bout.write(b);
                    }
                    Bout.close();
                    out.close();
                    System.out.println(Fout + "解压成功");
                }
                Bin.close();
                Zin.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                log.error("", e);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            log.error("", e);
        }

        long endTime = System.currentTimeMillis();
        System.out.println("耗费时间： " + (endTime - startTime) + " ms");
    }


    public static void unZipFile(String zipFineName, String filePath) {
        File file = new File(zipFineName);
        unZipFile(file, filePath);
    }


}
